package com.shizhanzhe.qdd.home.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.adapter.ApprenticeExplainAdapter;
import com.shizhanzhe.qdd.home.bean.StGrade;
import com.shizhanzhe.qdd.url.Path;
import com.shizhanzhe.qdd.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YJ
 * @date 2019/1/4
 */
public class ApprenticeAwardActivity extends Activity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.dengji)
    TextView dengji;
    @BindView(R.id.youxiao)
    TextView youxiao;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.no_data)
    ImageView noData;
    private ApprenticeExplainAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apprentice_award);
        ImmersionBar.with(this).statusBarColor(R.color.them).init();
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        topTitle.setText("等级规则");
//        num.setText(Html.fromHtml("你 的 有 效 徒 弟 : <b>"+getIntent().getIntExtra("yxtd",0)+"</b> 个"));
        num.setText(Html.fromHtml("<b>"+getIntent().getIntExtra("yxtd",0)+"</b> 个<br>有效徒弟"));
//        dengji.setText(Html.fromHtml("<font color=\"#ffffff\"><b>"+getIntent().getStringExtra("levelname")+"</b></font><br>等级"+UserBean.level));
        dengji.setText(Html.fromHtml("当前等级 : <b>"+UserBean.level+"</b> 级 ( <b>"+getIntent().getStringExtra("levelname")+"</b> )"));
        youxiao.setText(Html.fromHtml("¥ <font color=\"#ffffff\"><b>0.0</b></font><br>每有效徒弟奖励"));
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ApprenticeExplainAdapter(this);
        recycler.setAdapter(mAdapter);
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        taskList();
    }

    @OnClick(R.id.top_back)
    public void onViewClicked() {
        finish();
    }

    private void taskList() {
        OkHttpDownloadJsonUtil.downloadJson(ApprenticeAwardActivity.this, Path.STGRADE(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    try {
                        StGrade stGrade = gson.fromJson(json, StGrade.class);
                        if (stGrade.getData().size() > 0) {
                            mAdapter.updateData(1, stGrade.getData());
                            for (StGrade.DataBean bean:stGrade.getData()) {
                                if (UserBean.level== bean.getTdid()){
                                    youxiao.setText(Html.fromHtml("<font color=\"#000000\"><b>"+bean.getMoney()+"</b></font><br>每有效徒弟奖励"));
                                }
                            }
                        } else {
                            noData.setVisibility(View.VISIBLE);
                        }
                    } catch (Exception e){
                        e.printStackTrace();
                        Toast.makeText(ApprenticeAwardActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(ApprenticeAwardActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
